package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.OrderTable;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("findAllOrder")
    public JsonResult findAllOrder(
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "2")Integer pageSize,
            Integer orderId, String name) {
        List<OrderTable> orders = null;
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        if (orderId != null && !"".equals(orderId)) {
            //查询对应类型所有书籍
            orders = orderService.findByOrderId(orderId);
        } else if (name != null && !"".equals(name)) {
            //查询对应类型所有书籍
            orders = orderService.findByOrderName(name);
        } else {
            //查询所有书籍
            orders = orderService.findAllOrder();
        }
        if (orders == null) {
            JsonResult jsonResult = JsonResult.error();
            return jsonResult;
        }
        //封装到PageInfo对象中
        PageInfo<OrderTable> pageInfo = new PageInfo<>(orders);
        //返回JsonResult对象
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setData(pageInfo.getList());
        jsonResult.setCount(pageInfo.getTotal());
        return jsonResult;
    }
    @RequestMapping("deleteById")
    public void deleteById(Integer id) {
        orderService.deleteById(id);
    }
    @RequestMapping("updateOrder")
    public void updateOrder(OrderTable orderTable) {
        orderService.updateOrder(orderTable);
    }
}
